﻿Imports System.Data
Imports System.Data.SqlClient
Imports FreeTextBoxControls
Partial Class nchitietsp
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub

    Protected WithEvents DropDownList1 As System.Web.UI.WebControls.DropDownList
    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region
    Dim blnAddNew As Boolean
    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        Dim intID As Int16
        If Not Page.IsPostBack Then

            hideText.Text = -1
            blnAddNew = True
            Button1.Text = "Thêm mới"
            If IsNumeric(Request.QueryString("id")) Then
                intID = Request.QueryString("id")
                BindGrid(intID)
                If Request.QueryString("new") <> 1 Then
                    blnAddNew = False
                    Button1.Text = "Cập nhật"
                End If
            End If
            Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
            Dim sSql As String = "select idloaisp,(tenloaisp + '-' + convert(varchar(3),  (case when isnull(idvn,0)=0 then idloaisp else idvn end) )  ) as  tenloaisp from loaisp  order by idnn,idvn"
            myConn.Open()
            Dim myAdapter As New SqlDataAdapter(sSql, myConn)
            Dim myDataSet As New DataSet
            myAdapter.Fill(myDataSet, sSql)
            loaisp.DataSource = myDataSet
            loaisp.DataBind()

            sSql = "select idnhomsp,(tennhomsp + '-' + convert(varchar(3), (case when isnull(idvn,0)=0 then idnhomsp else idvn end) )  ) as  tennhomsp from nhomsp order by idnn, idvn,idnhomsp"
            myAdapter = New SqlDataAdapter(sSql, myConn)
            Dim myDataSet1 As New DataSet
            myAdapter.Fill(myDataSet1, sSql)
            nhomsp.DataSource = myDataSet1
            nhomsp.DataBind()
            sSql = "select idnn,ngonngu from ngonngu"
            myAdapter = New SqlDataAdapter(sSql, myConn)
            Dim myDataSet2 As New DataSet
            myAdapter.Fill(myDataSet2, sSql)
            idnn.DataSource = myDataSet2
            idnn.DataBind()
            myConn.Close()
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'On Error Resume Next
        Dim sSql As String
        If hideText.Text <= 0 Then
            sSql = "insert into sanpham (tensanpham,tieude,tukhoa,idloaisp,idnhomsp,idnn,gioithieu,hinhnho,hinhlon,TT,spmoi,thutu,thoigianbh,chuan,idvn,dadk,ngaytk,ngaydk,sodk,ngaytao,tk,ghichugia) " & _
                    "values(@tensanpham,@tieude,@tukhoa,@idloaisp,@idnhomsp,@idnn,@gioithieu,@hinhnho,@hinhlon,@TT,@spmoi,@thutu,@thoigianbh,@chuan,@idvn,@dadk,@ngaytk,@ngaydk,@sodk,getdate(),@tk,@ghichugia)"
            blnAddNew = True
        Else
            sSql = "update sanpham set" & _
            " tensanpham=@tensanpham,tieude=@tieude,tukhoa=@tukhoa,idnn=@idnn,gioithieu=@gioithieu" & _
            " ,idloaisp=@idloaisp,idnhomsp=@idnhomsp" & _
            " ,hinhnho=@hinhnho,hinhlon=@hinhlon,TT=@TT,spmoi=@spmoi,thutu=@thutu,chuan=@chuan,idvn=@idvn,thoigianbh=@thoigianbh" & _
            " ,dadk=@dadk,ngaytk=@ngaytk,ngaydk=@ngaydk,sodk=@sodk, tk=@tk,ghichugia=@ghichugia" & _
            " where idsanpham=@idsanpham"
            blnAddNew = False
        End If
        Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
        Dim MyCommand As SqlCommand
        myConn.Open()
        MyCommand = New SqlCommand(sSql, myConn)
        If Not blnAddNew Then
            MyCommand.Parameters.Add(New SqlParameter("@idsanpham", hideText.Text))
        End If
        MyCommand.Parameters.Add(New SqlParameter("@tensanpham", tensanpham.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tieude", tieude.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tukhoa", tukhoa.Text))
        MyCommand.Parameters.Add(New SqlParameter("@TT", TT.Checked))
        MyCommand.Parameters.Add(New SqlParameter("@spmoi", spmoi.Checked))
        MyCommand.Parameters.Add(New SqlParameter("@idnn", idnn.SelectedValue))
        MyCommand.Parameters.Add(New SqlParameter("@chuan", chuan.Text))
        MyCommand.Parameters.Add(New SqlParameter("@thutu", IIf(thutu.Text = "", 0, CInt(thutu.Text))))
        MyCommand.Parameters.Add(New SqlParameter("@thoigianbh", CInt(thoigianbh.Text)))
        MyCommand.Parameters.Add(New SqlParameter("@hinhnho", hinhnho.Text))
        MyCommand.Parameters.Add(New SqlParameter("@hinhlon", hinhlon.Text))
        MyCommand.Parameters.Add(New SqlParameter("@idvn", IIf(idvn.Text = "", 0, idvn.Text)))

        MyCommand.Parameters.Add(New SqlParameter("@dadk", DaDK.Checked))
        MyCommand.Parameters.Add(New SqlParameter("@ngaytk", VN2SysDate(NgayTK.Text)))
        MyCommand.Parameters.Add(New SqlParameter("@sodk", SoDK.Text))
        MyCommand.Parameters.Add(New SqlParameter("@tk", txtTK.Text))
        MyCommand.Parameters.Add(New SqlParameter("@ghichugia", Ghichugia.Text))
        MyCommand.Parameters.Add(New SqlParameter("@ngaydk", VN2SysDate(NgayDK.Text)))
        'MyCommand.Parameters.Add(New SqlParameter("@ngaydk", DBNull.Value))

        MyCommand.Parameters.Add(New SqlParameter("@gioithieu", gioithieu.Text))
        MyCommand.Parameters.Add(New SqlParameter("@idloaisp", loaisp.SelectedValue))
        MyCommand.Parameters.Add(New SqlParameter("@idnhomsp", nhomsp.SelectedValue))
        MyCommand.ExecuteNonQuery()
        If blnAddNew Then
            sSql = "select max(idsanpham) from sanpham"
            MyCommand.CommandText = sSql
            hideText.Text = MyCommand.ExecuteScalar
        End If
        sSql = "update sanpham set noidung =N'" & Replace(noidung.Text, "'", "''") & "' where idsanpham =" & hideText.Text
        MyCommand.CommandText = sSql
        MyCommand.ExecuteNonQuery()

        If idnn.SelectedValue > 1 And Request.QueryString("new") = 1 Then
            Dim idNhom, idLoai As Int32
            sSql = "select top 1 idvn from nhomsp where idnhomsp=" & nhomsp.SelectedValue
            MyCommand.CommandText = sSql
            idNhom = MyCommand.ExecuteScalar

            sSql = "select top 1 idvn from loaisp where idloaisp=" & loaisp.SelectedValue
            MyCommand.CommandText = sSql
            idLoai = MyCommand.ExecuteScalar
            Response.Redirect("dmsanpham.aspx?c=" & idLoai & "&g=" & idNhom)
        Else
            Response.Redirect("dmsanpham.aspx?c=" & loaisp.SelectedValue & "&g=" & nhomsp.SelectedValue)
        End If
        myConn.Close()

    End Sub
    Private Sub BindGrid(ByVal vId As Int16)
        On Error Resume Next
        Dim dr As DataRow
        Dim myConn As New SqlConnection(ConfigurationSettings.AppSettings("cons"))
        Dim sSql As String = "select top 1 idsanpham,idvn,tensanpham,tieude,tukhoa,idloaisp, idnhomsp,tensanpham,idnn,TT,thutu,gioithieu,noidung,hinhnho,hinhlon,thoigianbh,spmoi,chuan, dadk, convert(varchar(10),ngaytk,103) as ngaytk, convert(varchar(10),ngaydk,103) as ngaydk,sodk,tk,ghichugia from sanpham where idsanpham = " & vId & " order by thutu"
        myConn.Open()
        Dim myComm As New SqlCommand(sSql, myConn)
        Dim myDr As SqlDataReader
        myDr = myComm.ExecuteReader
        If myDr.Read Then
            tensanpham.Text = myDr("tensanpham")
            tieude.Text = myDr("tieude")
            tukhoa.Text = myDr("tukhoa")
            TT.Checked = myDr("TT")
            spmoi.Checked = myDr("spmoi")
            thutu.Text = myDr("thutu")
            gioithieu.Text = myDr("gioithieu")
            noidung.Text = myDr("noidung")
            idnn.SelectedValue = myDr("idnn")
            thoigianbh.Text = myDr("thoigianbh")
            loaisp.SelectedValue = myDr("idloaisp")
            nhomsp.SelectedValue = myDr("idnhomsp")
            hinhnho.Text = myDr("hinhnho")
            hinhlon.Text = myDr("hinhlon")
            chuan.Text = myDr("chuan")
            idvn.Text = myDr("idvn")
            DaDK.Checked = myDr("dadk")
            NgayTK.Text = myDr("ngaytk")
            NgayDK.Text = myDr("ngaydk")
            SoDK.Text = myDr("sodk")
            txtTK.Text = myDr("TK")
            Ghichugia.Text = myDr("ghichugia")

            hideText.Text = myDr("idsanpham")
            If Request.QueryString("new") = 1 Then
                If idnn.SelectedValue = 1 Then idvn.Text = hideText.Text
                hideText.Text = -1
                Button1.Text = "Thêm mới"
            End If
        End If
        myConn.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If idnn.SelectedValue = 1 Then idvn.Text = hideText.Text
        hideText.Text = -1
        Button1.Text = "Thêm mới"
    End Sub

  
End Class

